package com.listener;

import com.bean.SeatLog;
import com.service.SeatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener()
public class ExitListener implements HttpSessionListener {

    SeatService seatService;
    Logger logger = LoggerFactory.getLogger(ExitListener.class);

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        //logger.info(se.getSession().getId() + "  session创建了");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {

        seatService = WebApplicationContextUtils
                .getWebApplicationContext(se.getSession().getServletContext())
                .getBean(SeatService.class);
        HttpSession session = se.getSession();
        SeatLog seatUser = (SeatLog) session.getAttribute("seatUser");
        if (seatUser != null) {
            seatService.exit(seatUser);
        }
        //logger.info(se.getSession().getId() + (seatUser != null ? seatUser.getUserName() : "") + " 的session失效了");
    }
}
